package hotel.switchs.oversea.online.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.enums.MtGoodsEnums;
import hotel.base.oversea.dto.meituan.MtHotelGoodsDataGroupRoomDto;
import hotel.base.oversea.dto.meituan.MtHotelGoodsDto;
import hotel.base.oversea.dto.meituan.MtHotelRoomBedInfoDto;
import hotel.base.oversea.dto.meituan.MtHotelRoomDto;
import hotel.base.oversea.entity.MtHotelRoomVo;
import hotel.base.oversea.entity.master.CompareHotelBasicRoom;
import hotel.base.oversea.utils.*;
import hotel.base.oversea.vo.MtOrderCheckVo;
import hotel.base.oversea.vo.meituan.MtHotelDownVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsCheckFailOneVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsCheckFailVo;
import hotel.base.oversea.vo.meituan.request.goods.MtHotelGoodsStatusDetailStateVo;
import hotel.base.oversea.vo.meituan.request.room.MtHotelRealRoomVo;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsCancelRuleReturnVo;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsCancelRuleVo;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsInventoryVo;
import hotel.base.oversea.vo.meituan.response.goods.MtHotelGoodsPriceBaseDetailVo;
import hotel.switchs.oversea.api.entity.SwitchsNotifyShortLog;
import hotel.switchs.oversea.online.mapper.SwitchsNotifyShortLogMapper;
import hotel.switchs.oversea.online.utils.RestTemplateUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.*;

/**
 * 相关restTemplate 接口调用工具实现类
 *
 * @Author lmf
 * @Date 2020/8/5 10:29
 **/
@Slf4j
@Service
public class SwitchRestTempaleUtilsService {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * post 提交json格式
     * 获取房型名称
     */
    public Result sendPostRequestByRoomInfo(String url, Map<String, Object> params) {
        log.info("=====请求开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("==========获取房型名称 respStr{}", JSON.toJSONString(resultApi));
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(resultApi));
        MtHotelRoomDto mtHotelRoomDto = null;
        if (null != resultApi.getData()) {
            mtHotelRoomDto = JacksonUtil.parse(jsonObject.getString("data"), MtHotelRoomDto.class);
        }

        Result<MtHotelRoomDto> result = new Result<>();
        result.setData(mtHotelRoomDto);
        return result;
    }

    /**
     * 获取美团库存信息
     *
     * @param url
     * @param params
     * @return
     */
    public Result<MtHotelGoodsInventoryVo> sendPostRequestGetGoodsInventory(String url, Map<String, Object> params) {
        log.info("=====请求美团库存url为{}=====", url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("===========请求美团库存的参数为{}", JSON.toJSONString(params));
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        if (resultApi.getMsg().toString().contains("token")) {
            resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        }
        log.info("==========获取美团库存的参数 respStr{}", resultApi);
        JSONObject jsonObject= (JSONObject) JSON.parse(JSON.toJSONString(resultApi));
        MtHotelGoodsInventoryVo mtHotelGoodsInventoryVo = null;
        if (null != resultApi.getData()) {
            /*mtHotelGoodsInventoryVo = JacksonUtil.parse(resultApi.getData().toString(), MtHotelGoodsInventoryVo.class);*/
            mtHotelGoodsInventoryVo = JacksonUtil.parse(jsonObject.getString("data"), MtHotelGoodsInventoryVo.class);
        }
        return new Result<>(mtHotelGoodsInventoryVo);
    }

    /**
     * 获取床型列表
     *
     * @param url
     * @param params
     * @return
     */
    public Result sendPostRequestGetBed(String url, Map<String, Object> params) {
        log.info("=====请求开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("==========获取美团获取床型列表 respStr{}", JSON.toJSONString(resultApi));
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(resultApi));
        List<MtHotelRoomBedInfoDto> mtHotelRoomBedInfoDtos = new ArrayList<>();
        if (null != resultApi.getData()) {
            mtHotelRoomBedInfoDtos = JacksonUtil.jsonToList(jsonObject.getString("data"), MtHotelRoomBedInfoDto.class);
        }
        Result<List<MtHotelRoomBedInfoDto>> result = new Result<>(mtHotelRoomBedInfoDtos);
        return result;
    }

    /**
     * 发送post提交
     */
    public Result sendPostRequest(String url, Map<String, Integer> params) {
        log.info("=====请求美团产品接口开始,url为{}=====",url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=====请求美团的产品参数为{}", JSON.toJSONString(params));
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("==========获取美团的产品 respStr{}", JSON.toJSONString(resultApi));
        //List<MtHotelGoodsDto> mtHotelGoodsDtos = new ArrayList<>();
        Map<String, List<MtHotelGoodsDto>> mtHotelGoodsDataGroupRoomDtosMap = new HashMap<>();
        List<MtHotelGoodsDataGroupRoomDto> mtHotelGoodsDataGroupRoomDtos = new ArrayList<>();
        if (null != resultApi.getData()) {
            //mtHotelGoodsDtos =JacksonUtil.jsonToList(resultApi.getData().toString(),MtHotelGoodsDto.class);
            mtHotelGoodsDataGroupRoomDtos = JacksonUtil.jsonToList((String) resultApi.getData(), MtHotelGoodsDataGroupRoomDto.class);
            for (MtHotelGoodsDataGroupRoomDto dto : mtHotelGoodsDataGroupRoomDtos) {
                mtHotelGoodsDataGroupRoomDtosMap.put(dto.getMtRoomId(), dto.getMtHotelGoodsDtoData());
            }
        }
        Result<Map<String, List<MtHotelGoodsDto>>> result = new Result<>(mtHotelGoodsDataGroupRoomDtosMap);
        return result;
    }
    /**
     * 发送post提交
     */
    public Result sendPostRequestGetHotelGoodList(String url, Map<String, String> params) {
        log.info("=====请求美团产品接口开始,url为{}=====",url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=====请求美团的产品参数为{}", JSON.toJSONString(params));
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("==========获取美团的产品 respStr{}", JSON.toJSONString(resultApi));
        Map<String, List<MtHotelGoodsDto>> mtHotelGoodsDataGroupRoomDtosMap = new HashMap<>();
        Map<String, List<MtHotelGoodsDto>> stringListMap=new HashMap<>();
        if(!ObjectUtils.isEmpty(resultApi.getData())){
           stringListMap=GsonUtils.GsonToMaps(resultApi.getData().toString());
           for(Map.Entry<String, List<MtHotelGoodsDto>> set:stringListMap.entrySet()){
               List<MtHotelGoodsDto> mtHotelGoodsDtos=set.getValue();
               String json= JSONUtils.toJSONString(mtHotelGoodsDtos);
               mtHotelGoodsDtos=JacksonUtil.jsonToList(json,MtHotelGoodsDto.class);
               mtHotelGoodsDataGroupRoomDtosMap.put(set.getKey(),mtHotelGoodsDtos);
           }
        }
        Result<Map<String, List<MtHotelGoodsDto>>> result = new Result<>(mtHotelGoodsDataGroupRoomDtosMap);
        return result;
    }
    /**
     * 发送post提交
     */
    public Result sendPostRequest2(String url, Map<String, Object> params) {
        log.info("=====请求美团产品接口开始,url为{}=====",url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=====请求美团的产品参数为{}", JSON.toJSONString(params));
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("==========获取美团的产品 respStr{}", JSON.toJSONString(resultApi));
        //List<MtHotelGoodsDto> mtHotelGoodsDtos = new ArrayList<>();
        Map<String, List<MtHotelGoodsDto>> mtHotelGoodsDataGroupRoomDtosMap = new HashMap<>();
        List<MtHotelGoodsDataGroupRoomDto> mtHotelGoodsDataGroupRoomDtos = new ArrayList<>();
        if (null != resultApi.getData()) {
            //mtHotelGoodsDtos =JacksonUtil.jsonToList(resultApi.getData().toString(),MtHotelGoodsDto.class);
            mtHotelGoodsDataGroupRoomDtos = JacksonUtil.jsonToList((String) resultApi.getData(), MtHotelGoodsDataGroupRoomDto.class);
            for (MtHotelGoodsDataGroupRoomDto dto : mtHotelGoodsDataGroupRoomDtos) {
                mtHotelGoodsDataGroupRoomDtosMap.put(dto.getMtRoomId(), dto.getMtHotelGoodsDtoData());
            }
        }
        Result<Map<String, List<MtHotelGoodsDto>>> result = new Result<>(mtHotelGoodsDataGroupRoomDtosMap);
        return result;
    }

    /**
     * 发送post请求表单提交
     */
    public Result<MtHotelRoomVo> sendPostRequestByGetMtId(String url, Map<String, Integer> params) {
        log.info("=====请求开始效验控制器为url{}=====", url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Integer>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<MtHotelRoomVo>> typeRef = new ParameterizedTypeReference<Result<MtHotelRoomVo>>() {
        };
        log.info("==============请求参数为{}====", request.toString());
        ResponseEntity<Result<MtHotelRoomVo>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        if(responseEntity.getBody().getMsg().toString().contains("token")){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("============token失效再次进行重试===================");
            responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);

        }
        return responseEntity.getBody();
    }

    /**
     * 发送post请求表单提交
     */
    public Result<CompareHotelBasicRoom> sendPostRequestByGetSwitchHotelIdAndRoomId(String url, Map<String, Integer> params) {
        log.info("=====请求开始效验控制器为url{}=====", url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("=========请求效验房型信息接口返回数据为{}=========",resultApi);
        CompareHotelBasicRoom compareHotelBasicRoom=new CompareHotelBasicRoom();
        JSONObject jsonObject= (JSONObject) JSON.parse(JSON.toJSONString(resultApi));
        if (null != resultApi.getData()) {
            compareHotelBasicRoom = JacksonUtil.parse(jsonObject.getString("data"), CompareHotelBasicRoom.class);
        }
       /* HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Integer>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<CompareHotelBasicRoom>> typeRef = new ParameterizedTypeReference<Result<CompareHotelBasicRoom>>() {
        };
        log.info("==============请求参数为{}====", request.toString());
        ResponseEntity<Result<CompareHotelBasicRoom>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);*/
        return new Result<>(compareHotelBasicRoom);
    }

    /**
     * 获取美团的产品价格
     *
     * @param url
     * @param params
     * @return
     */
    public Result<List<MtHotelGoodsPriceBaseDetailVo>> sendPostRequestGetGoodsPrice(String url, Map<String, Object> params) {
        log.info("=====请求产品价格开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        //获取携程订单实时状态池信息
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("==========请求美团产品价格 respStr{}", JSON.toJSONString(resultApi));
        JSONObject jsonObject= (JSONObject) JSON.parse(JSON.toJSONString(resultApi));
        List<MtHotelGoodsPriceBaseDetailVo> list = new ArrayList<>();
        if (null != resultApi.getData()) {
           /* list = JacksonUtil.jsonToList(resultApi.getData().toString(), MtHotelGoodsPriceBaseDetailVo.class);*/
            list = JacksonUtil.jsonToList(jsonObject.getString("data"), MtHotelGoodsPriceBaseDetailVo.class);
        }
        return new Result<>(list);
    }

    /**
     * 获取产品的房态信息
     *
     * @param url
     * @param params
     * @return
     */
    public Result<Map<Integer, List<MtHotelGoodsStatusDetailStateVo>>> sendPostRequestGetGoodsStatus(String url, Map<String, Object> params) {
        log.info("=====请求房态开始=====");
        log.info("============请求的url路径为{}", url);
        //忽略401错误
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<Map<Integer, List<MtHotelGoodsStatusDetailStateVo>>>> typeRef = new ParameterizedTypeReference<Result<Map<Integer, List<MtHotelGoodsStatusDetailStateVo>>>>() {
        };
        log.info("================请求的参数为{}", request.toString());
        ResponseEntity<Result<Map<Integer, List<MtHotelGoodsStatusDetailStateVo>>>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }


    /**
     * 根据携程母物理房型id 美团物理房型获取对应的效验信息
     *
     * @param url
     * @param params
     * @return
     */
    public Result<CompareHotelBasicRoom> getCompareHotelBasicRoomInfo(String url, Map<String, Integer> params) {
        log.info("================请求效验控制器url为{}", url);
        //忽略401错误
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Integer>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<CompareHotelBasicRoom>> typeRef = new ParameterizedTypeReference<Result<CompareHotelBasicRoom>>() {
        };
        log.info("=============请求参数为{}", request.toString());
        ResponseEntity<Result<CompareHotelBasicRoom>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }

    /**
     * 根据携程母酒店id 获取美团的酒店id
     *
     * @param url
     * @param params
     * @return
     */
    public Result sendPOstRequestByGetMtHotelId(String url, Map<String, String> params) {
        log.info("=====请求美团酒店id=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result> typeRef = new ParameterizedTypeReference<Result>() {
        };
        ResponseEntity<Result> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }

    /**
     * 根据美团房型id获取产品列表
     *
     * @param url
     * @param params
     * @return
     */
    public Result<List<MtHotelGoodsDto>> sendPostRequestByGetGoodsList(String url, Map<String, Integer> params) {
        log.info("=====请求库存开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Integer>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<List<MtHotelGoodsDto>>> typeRef = new ParameterizedTypeReference<Result<List<MtHotelGoodsDto>>>() {
        };
        ResponseEntity<Result<List<MtHotelGoodsDto>>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }

    /**
     * 请求日志接口
     *
     * @param adminUrl
     * @param params
     */
    public Result<Object> sendPostRequestToAdmin(String adminUrl, Map<String, Object> params) {
        log.info("=====请求日志开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(adminUrl, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }

    /**
     * @author: lmf
     * @date: 2020/9/3 11:05
     * 推送记录请求到美团日志文件接口
     */
    @Autowired
    SwitchsNotifyShortLogMapper switchsNotifyShortLogMapper;

    public void pushSavePopLog(int hotelId, int mtHotelId, String timestampFromMtData) {

        SwitchsNotifyShortLog switchsNotifyShortLog = new SwitchsNotifyShortLog();
        switchsNotifyShortLog.setId(StringUtil.getUUID());
        switchsNotifyShortLog.setTimestamp(timestampFromMtData);
        switchsNotifyShortLog.setCreateTime(new Date());
        switchsNotifyShortLog.setMtHotelId(String.valueOf(mtHotelId));
        switchsNotifyShortLog.setSwHotelId(String.valueOf(hotelId));
        switchsNotifyShortLog.setOprate(MtGoodsEnums.goodsChangeType.POP.getCode());
        // 10 异步价格  11 主动查询价格 20 异步房态  21 主动查询房态 30 携程推送成功数据
        switchsNotifyShortLog.setSource(MtGoodsEnums.goodsChangeSource.SWITCHS_PUSH.getCode());
        try {
            switchsNotifyShortLogMapper.insert(switchsNotifyShortLog);
        } catch (Exception e) {
            log.info("异步通知有变化数据保存日志switchsNotifyShortLog异常{}", JSON.toJSONString(switchsNotifyShortLog));
            e.printStackTrace();
        }

        /*Map map= new HashMap();
        try{
            map.put("hotelId",String.valueOf(hotelId));
            map.put("mtHotelId",String.valueOf(mtHotelId));
            map.put("timestamp",timestampFromMtData);
            //发起请求
            Result response = RestTemplateUtils.post(CommonUrlConstants.MEITUAN_HOST+"/mtNotifyShortLog/popSaveLog", JSON.toJSONString(map),restTemplate);
            if(CommonConstants.SUCCESS==response.getCode()){
            }else{
                log.error("////////////////////////pushRoom 保存队列日志异常：{},{}",JSON.toJSONString(map),JSON.toJSONString(response));
            }
        }catch (Exception e){
            log.error("pushRoom {} >>>>>>>>> 保存队列日志   异常catch：{},{},{}", DateUtil.getNowTime(),JSON.toJSONString(map),e.getMessage());
            e.printStackTrace();
        }*/


    }

    /**
     * @author: lmf
     * @date: 2020/9/15 14:51
     */
    public Result<List<MtHotelGoodsCancelRuleVo>> sendPostRequestGetCancelByMtHotelIdAndGoodId(String url, Map<String, Object> params) {
        log.info("=====请求日志开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<List<MtHotelGoodsCancelRuleVo>>> typeRef = new ParameterizedTypeReference<Result<List<MtHotelGoodsCancelRuleVo>>>() {
        };
        ResponseEntity<Result<List<MtHotelGoodsCancelRuleVo>>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }
    /***
    * @Author: lmf
    * @Date: 2020/9/24
     * 爬取美团的房态房价更新携程的售卖房型
    */
    public Result<Object> sendPostRequestToCompare(String url, Map<String, List<String>> map) {
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, List<String>>> request = new HttpEntity<>(map, headers);
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }

    /**
     *
     * @param url
     * @param map
     * @return
     */
    @SneakyThrows
    public  MtOrderCheckVo orderCheck(String url, Map<String, Object> map) {
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============调用下单前效验的请求参数为{}==============", JSON.toJSONString(map));
        //获取携程订单实时状态池信息
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(map), restTemplate);
        if(ObjectUtils.isEmpty(resultApi)){
            Thread.sleep(200);
            resultApi = RestTemplateUtils.post(url, JSON.toJSONString(map), restTemplate);
        }
        if(ObjectUtils.isEmpty(resultApi.getData())){
            Thread.sleep(200);
            resultApi = RestTemplateUtils.post(url, JSON.toJSONString(map), restTemplate);
            if(ObjectUtils.isEmpty(resultApi.getData())){
                Thread.sleep(200);
                resultApi = RestTemplateUtils.post(url, JSON.toJSONString(map), restTemplate);
            }
        }
        log.info("=============调用下单前效验返回的数据为{}=============",resultApi);
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(resultApi));
        MtOrderCheckVo mtOrderCheckVo=JacksonUtil.parse(jsonObject.getString("data"),MtOrderCheckVo.class);
        return mtOrderCheckVo;

    }

    /**
     * 效验酒店是否已经下线
     * @param url
     * @param map
     * @return
     */
    public Result<List<MtHotelDownVo>> sendReuestToMtCheckHotelFailure(String url, Map<String, Object> map) {
        log.info("=============请求的url为{}",url);
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============调用效验酒店是否已经下线请求参数为{}==============", JSON.toJSONString(map));
        //获取携程订单实时状态池信息
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(map), restTemplate);
        log.info("============请求返回的数据为{}",JacksonUtil.objectToJson(resultApi));
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(resultApi));
        List<MtHotelDownVo> list=JacksonUtil.jsonToList(jsonObject.getString("data"),MtHotelDownVo.class);
        return new Result<>(list);
    }

    /**
     * 效验美团酒店是否失效
     * @param url
     * @param mtHotelGoodsCheckFailVo
     * @return
     */
    public Result<List<MtHotelGoodsCheckFailOneVo>> sendReuestToMtCheckHotelGoodFailure(String url, MtHotelGoodsCheckFailVo mtHotelGoodsCheckFailVo) {
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============调用效验美团酒店产品是否失效请求参数为{}==============", JSON.toJSONString(mtHotelGoodsCheckFailVo));
        //获取携程订单实时状态池信息
        Result resultApi = RestTemplateUtils.post(url, JSON.toJSONString(mtHotelGoodsCheckFailVo), restTemplate);
        log.info("============请求返回的数据为{}",JacksonUtil.objectToJson(resultApi));
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(resultApi));
        List<MtHotelGoodsCheckFailOneVo> mtHotelGoodsCheckFailOneVos=JacksonUtil.jsonToList(jsonObject.getString("data"),MtHotelGoodsCheckFailOneVo.class);
        return new Result<>(mtHotelGoodsCheckFailOneVos);
    }

    /**
     * 根据携程母物理房型+美团的房型id获取是否有效验数据
     * @param url
     * @param params
     * @return
     */
    public Result<CompareHotelBasicRoom> sendPostRequestToCompareToGetRoomInfo(String url, Map<String, Integer> params) {
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============根据携程母物理房型+美团的房型id获取是否有效验数据请求参数为{}==============", JSON.toJSONString(params));
        //获取携程订单实时状态池信息
        Result result = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("============请求返回的数据为{}",JacksonUtil.objectToJson(result));
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(result));
        CompareHotelBasicRoom compareHotelBasicRoom=JacksonUtil.parse(jsonObject.getString("data"),CompareHotelBasicRoom.class);
        return new Result<>(compareHotelBasicRoom);
    }

    /**
     * 根据美团酒店id+美团的房型id获取是否有效验数据
     * @param url
     * @param params
     * @return
     */
    public Result<CompareHotelBasicRoom> sendPostRequestToCompareToGetRoomInfoByMtHotelIdAndMtRoomId(String url, Map<String, String> params) {
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============根据美团酒店id+美团的房型id获取是否有效验数据请求参数为{}==============", JSON.toJSONString(params));
        //获取携程订单实时状态池信息
        Result result = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("============请求返回的数据为{}",JacksonUtil.objectToJson(result));
        JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(result));
        CompareHotelBasicRoom compareHotelBasicRoom=JacksonUtil.parse(jsonObject.getString("data"),CompareHotelBasicRoom.class);
        return new Result<>(compareHotelBasicRoom);
    }

    /**
     *  根据美团酒店id+美团的房型id删除效验数据
     * @param url
     * @param params
     * @return
     */
    public Result<Object> sendPostRequestToCompareToRemoveRoomInfo(String url, Map<String, Integer> params) {
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============根据美团酒店id+美团的房型id获取是否有效验数据请求参数为{}==============", JSON.toJSONString(params));
        //获取携程订单实时状态池信息
        Result result = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("============请求返回的数据为{}",JacksonUtil.objectToJson(result));
        return result;
    }

    /**
     * 让酒店进行房型匹配
     * @param url
     * @param params
     * @return
     */
    public Result<Object> sendPostRequestToCompareToCompareHotelRoomId(String url, Map<String, Integer> params) {
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        log.info("=============根据美团酒店id+美团的房型id获取是否有效验数据请求参数为{}==============", JSON.toJSONString(params));
        //获取携程订单实时状态池信息
        Result result = RestTemplateUtils.post(url, JSON.toJSONString(params), restTemplate);
        log.info("============请求返回的数据为{}",JacksonUtil.objectToJson(result));
        return result;
    }
     /***
     * @Author: lmf
     * @Date: 2020/12/11
      * 获取子酒店id
     */
    public Result<Integer> sendPOstRequestByGetHotelId(String url, Map<String, String> params) {
        log.info("=====请求获取子酒店id开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<Integer>> typeRef = new ParameterizedTypeReference<Result<Integer>>() {
        };
        ResponseEntity<Result<Integer>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        return responseEntity.getBody();
    }
    /***
    * @Author: lmf
    * @Date: 2020/12/15
     * 发起取消规则
    */
    public Result<List<MtHotelGoodsCancelRuleReturnVo>> sendPostRequestGetGoodsCancelRule(String url, Map<String, Object> params) {
        log.info("=====请求美团的取消规则接口开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
        log.info("==========请求接口参数为{}", JSON.toJSONString(params));
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        log.info("==============返回的数据为{}",responseEntity);
        List<MtHotelGoodsCancelRuleReturnVo> mtHotelGoodsCancelRuleReturnVos=JacksonUtil.jsonToList(responseEntity.getBody().getData().toString(),MtHotelGoodsCancelRuleReturnVo.class);
        return new Result<>(mtHotelGoodsCancelRuleReturnVos);
    }
    /***
    * @Author: lmf
    * @Date: 2021/1/12
    */
    public Result sendPostRequestToMeiTuanIsAciive(String url, Map<String, String> param) {
        log.info("=====请求美团的活动酒店接口开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> request = new HttpEntity<>(param, headers);
        log.info("==========请求接口参数为{}", JSON.toJSONString(param));
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        log.info("==============返回的数据为{}",responseEntity);
        return responseEntity.getBody();
    }

    /***
    * @Author: lmf
    * @Date: 2021/3/8
    */
    public Result sendPutToQuartModifyCrontab(String url, Map<String, String> param) {
        log.info("=====请求美团的活动酒店接口开始=====");
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> request = new HttpEntity<>(param, headers);
        log.info("==========请求接口参数为{}", JSON.toJSONString(param));
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        url=url+"?jobGroupName={jobGroupName}&jobClassName={jobClassName}&cronExpression={cronExpression}";
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, null, typeRef,param);
        log.info("==============返回的数据为{}",responseEntity);
        return responseEntity.getBody();
    }
    /***
    * @Author: lmf
    * @Date: 2021/4/6
     * 请求美团的房型
    */
    public Result<List<MtHotelRealRoomVo>> sendPostToMeiTuanHotelRoomReturnList(String url, Map<String, String> params) {
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers);
        log.info("==========请求接口参数为{}", JSON.toJSONString(params));
        ParameterizedTypeReference<Result<List<MtHotelRealRoomVo>>> typeRef = new ParameterizedTypeReference<Result<List<MtHotelRealRoomVo>>>() {
        };
        ResponseEntity<Result<List<MtHotelRealRoomVo>>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        log.info("==============返回的数据为{}",responseEntity);
        return responseEntity.getBody();
    }

    /***
     * @Author: lmf
     * @Date: 2021/4/14
     *
     */
    public Result<Object> sendPostRequesToMsg(String url, Map<String, String> params) {
        //忽略401错误
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                if (response.getRawStatusCode() != 401) {
                    super.handleError(response);
                }
            }
        });
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers);
        ParameterizedTypeReference<Result<Object>> typeRef = new ParameterizedTypeReference<Result<Object>>() {
        };
        ResponseEntity<Result<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, typeRef);
        log.info("==============返回的数据为{}",responseEntity);
        return responseEntity.getBody();
    }
}
